﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using TinyEngine.Core;
using TinyEngine.Core.Services;
using TinyEngineEditor.SceneViewer;
using System.ComponentModel;
using TinyEngine.Core.Entities;
using System.IO;

using TinyEngineEditor.ViewModels;
using TinyEngine.Editor.Extensibility;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace TinyEngineEditor
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Trace.WriteLine("Starting TinyEngine Editor");

            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            EditorSession.LaunchDialogRequest = LaunchWindowRequest;
        }

        private void LaunchWindowRequest(UserControl ctrl)
        {
            WindowHost.Launch(ctrl);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Trace.WriteLine("TinyEngine Editor Main Window Loaded");
        }

        private void sceneViewer1_FinishedLoading_1()
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                Dispatcher.BeginInvoke(new Action(InitPlugins));
            }    
        }

        void MainWindowControlAdded(UserControl control, Dock side)
        {
            if (side == Dock.Left)
                projectsContent.Content = control;
            if (side == Dock.Right)
                propertiesContent.Content = control;

            control.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            control.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
        }

        void InitPluginsBackground()
        {
            Thread.Sleep(500);

            Trace.WriteLine("Beginning initialization of plugins");

            EditorSession.Instance.MainMenu = this.menuMain;

            PluginManager.MainWindowControlAdded = MainWindowControlAdded;
            PluginManager.LoadPlugins();

            Dispatcher.BeginInvoke(new Action(InitEditor));
        }

        void InitPlugins()
        {
            Parallel.Invoke(InitPluginsBackground);
            SplashScreen.Start(this);
        }
        
        void InitEditor()
        {
            ApplicationSettings.Load();

            EditorSession.Init(SceneViewer.SceneViewer.Graphics, SceneViewer.SceneViewer.Services, delegate
            {
                this.sceneViewer1.FinishedInit();
                this.sceneViewer1.ForceRedraw();

                CloseSplash();
            });
        }

        private void CloseSplash()
        {
            Dispatcher.BeginInvoke(new Action(
                    delegate
                    {
                        SplashScreen.End();
                    }));
        }
        
        private void MenuNew_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Not yet implemented");
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            ApplicationSettings.Save();

            EditorSession.Save();
            base.OnClosing(e);
        }
    }
}
